package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.KV;
import com.ymatou.iapi.liveseller.parameter.ProductChangeCallBackBean;
import com.ymatou.iapi.liveseller.service.ProductChangeCallBackCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
/**
*商品修改回调
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ProductChangeCallBack{
	private static ProductChangeCallBackBean productchangecallbackBean;
	private static ProductChangeCallBackCall productchangecallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品修改回调");
	}

	@Before
	public void caseUp() {
		productchangecallbackBean=new ProductChangeCallBackBean();
		productchangecallbackCall=new ProductChangeCallBackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(productchangecallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProductChangeCallBack_001() {
		Logger.start(true,"商品修改回调,成功,title包含关键字修改万能链");
		try {
			List<String> lw=Arrays.asList("链接","专拍","直播专拍","直播专用","连接");
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			List<String> pids=new ArrayList<String>();
//			addpro.getProduct().setProductName(lw.get(new Random().nextInt(lw.size()))+addpro.getProduct().getProductName());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			pids.add(pid);
//			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pids);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), uinfo.getUserId(), pid);
			LiveRoomWapper liveRoomWapper=new LiveRoomWapper();
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(false, listm.get(0).get("linkword"), "验证linkword");
			
			productchangecallbackBean.setProductId(pid);
			List<KV> changeSet=new ArrayList<KV>();
			KV e=new KV();
			e.setKey("Title");
			e.setValue(lw.get(new Random().nextInt(lw.size()))+addpro.getProduct().getProductName());
			changeSet.add(e);
			productchangecallbackBean.setChangeSet(changeSet);
//			String send=FormatUtil.beanToGSONString(productchangecallbackBean);
			productchangecallbackCall.setData(productchangecallbackBean);
			productchangecallbackCall.callService();			
			listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(true, listm.get(0).get("linkword"), "验证linkword");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ProductChangeCallBack_002() {
		Logger.start(true,"商品修改回调,成功,title不包含关键字设置商品不是万能链");
		try {
			List<String> lw=Arrays.asList("链接","专拍","直播专拍","直播专用","连接");
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			addpro.getProduct().setProductName(lw.get(new Random().nextInt(lw.size()))+addpro.getProduct().getProductName());
			List<String> pids=new ArrayList<String>();
//			addpro.getProduct().setProductName(lw.get(new Random().nextInt(lw.size()))+addpro.getProduct().getProductName());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			pids.add(pid);
//			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pids);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), uinfo.getUserId(), pid);
			LiveRoomWapper liveRoomWapper=new LiveRoomWapper();
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(true, listm.get(0).get("linkword"), "验证linkword");
			
			productchangecallbackBean.setProductId(pid);
			List<KV> changeSet=new ArrayList<KV>();
			KV e=new KV();
			e.setKey("Title");
			e.setValue("pname");
			changeSet.add(e);
			productchangecallbackBean.setChangeSet(changeSet);
//			String send=FormatUtil.beanToGSONString(productchangecallbackBean);
			productchangecallbackCall.setData(productchangecallbackBean);
			productchangecallbackCall.callService();			
			listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(false, listm.get(0).get("linkword"), "验证linkword");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}